package com.kould.common.adapter;

import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;

import java.io.IOException;

public class IdAdapter extends TypeAdapter<Long> {

    /**
     * 序列化
     *
     * @param out   输出流
     * @param value 值
     * @throws IOException
     */
    @Override
    public void write(JsonWriter out, Long value) throws IOException {
        if (value != null) {
            out.value(value.toString());
        }
    }

    /**
     * 反序列化
     *
     * @param in 输入流
     * @return
     * @throws IOException
     */
    @Override
    public Long read(JsonReader in) throws IOException {
        String s = in.nextString();
        if (!"".equals(s)) {
            return Long.valueOf(s);
        } else {
            return null;
        }
    }
}
